package com.amanda.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.amanda.dao.MemberDao;
import com.amanda.dao.OrderDao;
import com.amanda.service.ReportService;
import com.amanda.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author amanda
 * @Description 统计报表业务
 */

@Transactional
@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> getBusinessReport() throws Exception {
        /**
         * 获得运营统计数据
         * Map数据格式：
         *      todayNewMember -> number 今日新增会员数
         *      totalMember -> number 总会员数
         *      thisWeekNewMember -> number 本周新增会员数
         *      thisMonthNewMember -> number 本月新增会员数
         *      todayOrderNumber -> number 今日预约数
         *      todayVisitsNumber -> number 今日到诊数
         *      thisWeekOrderNumber -> number 本周预约数
         *      thisWeekVisitsNumber -> number 本周到诊数
         *      thisMonthOrderNumber -> number 本月预约数
         *      thisMonthVisitsNumber -> number 本月到诊数
         *      hotSetmeals -> List<Setmeal>
         */
        Map<String, Object> map = new HashMap<>();
        // 当前日期
        String reportDate = DateUtils.parseDate2String(DateUtils.getToday());
        // 当前周周一日期
        String monday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        // 当前月1号日期
        String firstInMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        // 1.当前日期
        map.put("reportDate", reportDate);
        // 2.当天新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(reportDate);
        map.put("todayNewMember", todayNewMember);
        // 3.总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        map.put("totalMember", totalMember);
        // 4.当前一周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(monday);
        map.put("thisWeekNewMember", thisWeekNewMember);
        // 5.当前月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstInMonth);
        map.put("thisMonthNewMember", thisMonthNewMember);
        // 6.当天预约人数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(reportDate);
        map.put("todayOrderNumber", todayOrderNumber);
        // 7.当天就诊人数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(reportDate);
        map.put("todayVisitsNumber", todayVisitsNumber);
        // 8.本周预约人数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(monday);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        // 9.本周就诊人数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(monday);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        // 10.本月预约人数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstInMonth);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        // 11.本月就诊人数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstInMonth);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        // 12.热门套餐
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        map.put("hotSetmeal", hotSetmeal);
        return map;
    }
}
